package com.zkh.myutils.io.log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import com.zkh.myutils.utils.UtilsException;

/**
 * 日志打印流
 * @author zkh
 */
class LogPrintStream extends PrintStream{
	/** 原始构造器全部置为过时，均不使用 **/
	@Deprecated
	public LogPrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
		super(file, csn);
	}
	@Deprecated
	public LogPrintStream(File file) throws IOException {
		super(file);
	}
	@Deprecated
	public LogPrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException {
		super(out, autoFlush, encoding);
	}
	@Deprecated
	public LogPrintStream(OutputStream out, boolean autoFlush) {
		super(out, autoFlush);
	}
	@Deprecated
	public LogPrintStream(OutputStream out) throws FileNotFoundException, IOException {
		super(out);
	}
	@Deprecated
	public LogPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
		super(fileName, csn);
	}
	@Deprecated
	public LogPrintStream(String fileName) throws FileNotFoundException {
		super(fileName);
	}
	
	//原打印对象
	private static PrintStream orginStreamOut = System.out;
	private static PrintStream orginStreamErr = System.err;
	//当前使用流
	private PrintStream currentStream;
	//日志写文件流
	private BufferedWriter bw;

	/**
	 * 获取一个日志打印流，取缔原构造器
	 * @param writer 日志文件写句柄
	 * @param type 输出流类型，包括错误流和标准流两类，根据常量取值
	 * @return
	 */
	public static LogPrintStream getPrintStream(int type) throws IOException {
		//创建日志流（这里的参数内部不会使用，只要保证构造方法不报错（如传null会报空指针异常）即可）
		LogPrintStream logPrintStream = new LogPrintStream(System.out);
		//设置类型
		logPrintStream.currentStream = type==0 ? orginStreamErr : orginStreamOut;
		//返回
		return logPrintStream;
	}
	
	/**
	 * 保存写对象
	 * @param uuid 唯一标识
	 * @param writer 日志写句柄
	 */
	public void setBufferedWriter(BufferedWriter writer) {
		this.bw = writer;
	}
	
	/**
	 * 写日志
	 * @param msg
	 */
	private void write(Object msg) {
		//不为空，写日志
		if(bw!=null) {
			try {
				bw.write(msg==null ? "null" : msg.toString());
				bw.flush();
			} catch (IOException e) {
				throw new UtilsException(e);
			}
		}
	}
	
	/**
	 * 换行
	 */
	public void newLine() {
		//不为空，输出换行
		if(bw!=null) {
			try {
				bw.newLine();
				bw.flush();
			} catch (IOException e) {
				throw new UtilsException(e);
			}
		}
	}
	
	/**
	 * 写日志
	 * @param msg
	 */
	public void writeln(Object msg) {
		//不为空，输出换行
		if(bw!=null) {
			synchronized (msg) {
				write(msg);
				newLine();
			}
		}
	}
	
	/** 常用打印方法 **/
	public void print(boolean b) {
		currentStream.print(b);
		write(b);
	}
	public void print(char c) {
		currentStream.print(c);
		write(c);
	}
	public void print(int i) {
		currentStream.print(i);
		write(i);
	}
	public void print(long l) {
		currentStream.print(l);
		write(l);
	}
	public void print(float f) {
		currentStream.print(f);
		write(f);
	}
	public void print(double d) {
		currentStream.print(d);
		write(d);
	}
	public void print(char[] s) {
		currentStream.print(s);
		write(s);
	}
	public void print(String s) {
		currentStream.print(s);
		write(s);
	}
	public void print(Object obj) {
		currentStream.print(obj);
		write(obj);
	}
	public void println() {
		currentStream.println();
		newLine();
	}
	public void println(boolean x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(char x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(int x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(long x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(float x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(double x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(char[] x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(String x) {
		currentStream.println(x);
		writeln(x);
	}
	public void println(Object x) {
		currentStream.println(x);
		writeln(x);
	}
	
	/** 其它不使用的方法 **/

	public void flush() {
		currentStream.flush();
	}
	public void close() {
		currentStream.close();
	}
	public boolean checkError() {
		return currentStream.checkError();
	}
	protected void setError() {
		super.setError();
	}
	protected void clearError() {
		super.clearError();
	}
	public void write(int b) {
		currentStream.write(b);
	}
	public void write(byte[] buf, int off, int len) {
		currentStream.write(buf, off, len);
	}
	public PrintStream printf(String format, Object... args) {
		return currentStream.printf(format, args);
	}
	public PrintStream printf(Locale l, String format, Object... args) {
		return currentStream.printf(l, format, args);
	}
	public PrintStream format(String format, Object... args) {
		return currentStream.format(format, args);
	}
	public PrintStream format(Locale l, String format, Object... args) {
		return currentStream.format(l, format, args);
	}
	public PrintStream append(CharSequence csq) {
		return currentStream.append(csq);
	}
	public PrintStream append(CharSequence csq, int start, int end) {
		return currentStream.append(csq, start, end);
	}
	public PrintStream append(char c) {
		return currentStream.append(c);
	}
}
